package com.loong.life.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.common.rabbitmq.dto.base.BusinessEventDto;
import com.loong.common.rabbitmq.dto.base.RepositoryEventDto;
import com.loong.common.utils.TransformUtils;
import com.loong.global.dto.PhoneChargeDTO;
import com.loong.life.common.backend.param.PhoneChargeQueryParam;
import com.loong.life.mapper.PhoneChargeRecordMapper;
import com.loong.life.model.PhoneChargeRecord;
import com.loong.life.mq.PhoneChargeReport;
import com.loong.life.param.ChargeRecordQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: 刘群
 * @Date: 2020/11/26 16:30
 */
@Slf4j
@Repository
public class PhoneChargeRecordBusiness extends AbstractBusiness<PhoneChargeRecord> {

    @Resource
    private PhoneChargeRecordMapper chargeRecordMapper;

    @Resource
    private PhoneChargeReport chargeReport;

    /**
     * 根据参数查询手机充值记录
     * @param param 查询参数
     * @return 充值记录集合
     */
    public List<PhoneChargeRecord> findPhoneChargeRecordByParam(PhoneChargeQueryParam param){
        ChargeRecordQueryParam queryParam = new ChargeRecordQueryParam();
        if (ObjectUtils.isNotEmpty(param)){
            queryParam = TransformUtils.transform(param,ChargeRecordQueryParam.class);
        }
        //封装查询时间
        this.packingQueryTime(queryParam);
        return chargeRecordMapper.findChargeRecordByParam(queryParam);
    }

    /**
     * 封装查询时间
     * @param queryParam 查询参数
     */
    private void packingQueryTime(ChargeRecordQueryParam queryParam) {
        Date queryStartTime;
        Date queryEndTime = null;
        //获取上一年的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        Date lastYearDate = calendar.getTime();
        //若参数传过来的起始时间为空，或者小于上一年的时间，则查询时间为上一年的时间。否则查询时间为参数传过来的起始时间。
        if (ObjectUtils.isEmpty(queryParam.getStartTime()) || lastYearDate.getTime() > queryParam.getStartTime().getTime()) {
            queryStartTime = lastYearDate;
        }else {
            queryStartTime = queryParam.getStartTime();
        }
        //c端只传过来年月日0时0分0秒，因此需要将时间加1天
        if (ObjectUtils.isNotEmpty(queryParam.getEndTime())){
            Calendar time = Calendar.getInstance();
            time.setTime(queryParam.getEndTime());
            time.add(Calendar.DATE,1);
            queryEndTime = time.getTime();
        }
        queryParam.setStartTime(queryStartTime);
        queryParam.setEndTime(queryEndTime);
    }

    @Override
    public void save(PhoneChargeRecord model) {
        //新增手机充值记录
        super.save(model);
        //上报到MQ中
        PhoneChargeDTO chargeDTO = PhoneChargeDTO.builder()
                .id(model.getId())
              //  .chargeStatus(model.getChargeStatus())
                .build();
        log.debug("上报到MQ中:{}",model.getId());
        BusinessEventDto<Object> eventDTO = BusinessEventDto.builder().data(chargeDTO).build();
          chargeReport.report(eventDTO);
    }
}
